const app = getApp()
const { formatObjToUrl } = require('../../utils/util')

// pages/questions/questions.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 试题等级
    level: -1,
    // 试题列表
    questions: [],
    // 套数ID
    suitId: '',
    // 试题类型
    suitType: '',
    // 第几题
    questionIndex: 0,
    // 当前题目
    currentQuestion: {},
    // 正确个数
    rightCount: 0,
    // 错误个数
    errorCount: 0,
    // 是否可以答题
    canAnswer: true,
    // 显示答题卡
    showQuestionCard: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      suitId: options.suitId,
      suitType: options.suitType,
      level: options.level
    })
    // 获取题目
    this.getQuestions()
  },

  // 获取题目
  getQuestions() {
    const submitData = {
      subject_it: this.data.suitId,
      subject_type: this.data.suitType,
      level: this.data.level
    }
    app.axios({
      url: `/study/subjects/subjects${formatObjToUrl(submitData)}`
    }).then(res => {
      if(res.code === 0) {
        this.setData({
          questions: res.result.map(item => {
            return Object.assign({}, item, { isAnswered: false, myAnswer: '' })
          })
        })
        // 题目初始化（只针对选择题）
        this.initQuestion()
      }
    })
  },

  // 题目初始化（只针对选择题）
  initQuestion() {
    let cur_question = this.data.questions[this.data.questionIndex]
    this.setData({
      currentQuestion: cur_question
    })
  },

  // 显示答题卡
  openQuestionCard() {
    this.setData({
      showQuestionCard: true
    })
  },

  // 关闭答题卡
  closeQuestionCard() {
    this.setData({
      showQuestionCard: false
    })
  },

  // 点击答题卡跳转到某题
  jumpToQuestion(e) {
    this.setData({
      questionIndex: e.currentTarget.dataset.qindex
    })
    this.initQuestion();
  },

  // 上一题
  backQuestion() {
    if(this.data.questionIndex > 0) {
      let newIndex = this.data.questionIndex - 1
      this.setData({
        questionIndex: newIndex
      })
      this.initQuestion()
    }
  },

  // 下一题
  nextQuestion() {
    if(this.data.questionIndex < this.data.questions.length - 1) {
      let newIndex = this.data.questionIndex + 1
      this.setData({
        questionIndex: newIndex
      })
      this.initQuestion()
    }
  },

  // 答题
  handleAnswer(e) {
    if(this.data.currentQuestion.isAnswered) return false;
    const submitData = {
      option: e.currentTarget.dataset.answer,
      subject_id: this.data.currentQuestion.id
    }
    app.axios({
      url: `/answer/subject_answer${formatObjToUrl(submitData)}`
    }).then(res => {
      if(res.code === 0) {
        this.data.questions[this.data.questionIndex].isAnswered = true;
        this.data.questions[this.data.questionIndex].myAnswer = e.currentTarget.dataset.answer;
        // 统计对错个数
        if(e.currentTarget.dataset.answer == this.data.currentQuestion.answer) {
          this.setData({ rightCount: this.data.rightCount + 1 })
        } else {
          this.setData({ errorCount: this.data.errorCount + 1 })
        }
        this.initQuestion();
      }
    })
  }

})